find_package(Perl REQUIRED)

find_package(UnixCommands)
if (NOT GZIP)
  message(FATAL_ERROR "Can't find gzip")
endif (NOT GZIP)

option (USE_FATVERB "Add objective kinuyim to verbs" OFF)
if (USE_FATVERB)
  set(SEDCMD "'s/\\+//'")
else (USE_FATVERB)
  set(SEDCMD "'/\\+/d'")
endif (USE_FATVERB)  

  
set(SCRIPTS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../scripts)
set(DATA_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(EXE_DIR ${CMAKE_CURRENT_BINARY_DIR}/../src)
set(GZDICTS dout.nouns.wolig.gz dout.verbs.gz dout.nouns.shemp.gz)
set(DICTS milot extrawords biza-verbs biza-nouns)


# not sure why I can't simply test for ENV{PERL5LIB} in the "if"
set(PERL5LIB $ENV{PERL5LIB})
if (PERL5LIB)
  set(PERL5LIB $ENV{PERL5LIB}:${SCRIPTS_DIR})
else (PERL5LIB)
  set(PERL5LIB ${SCRIPTS_DIR})
endif (PERL5LIB)

# dout.nouns.wolig.gz
# dout.nouns.%:  %.dat wolig.pl
add_custom_command(
  OUTPUT dout.nouns.wolig.gz
  COMMAND ${PERL_EXECUTABLE} -w ${SCRIPTS_DIR}/wolig.pl -d ${DATA_DIR}/wolig.dat | ${GZIP} -4 > dout.nouns.wolig.gz
  DEPENDS ${DATA_DIR}/wolig.dat ${SCRIPTS_DIR}/wolig.pl
  )

# dout.verbs.gz
# dout.verbs.gz: woo woo.dat
add_custom_command(
  OUTPUT dout.verbs.gz shemp.dat
  COMMAND ${PERL_EXECUTABLE} -w ${SCRIPTS_DIR}/woo -v -o shemp.dat -i ${DATA_DIR}/woo.dat | sed ${SEDCMD} | ${GZIP} -4 > dout.verbs.gz
  DEPENDS ${DATA_DIR}/woo.dat ${SCRIPTS_DIR}/woo
  )

# dout.nouns.shemp.gz
#dout.nouns.shemp.gz:  shemp.dat wolig.pl
add_custom_command(
  OUTPUT dout.nouns.shemp.gz
  COMMAND ${PERL_EXECUTABLE} -w ${SCRIPTS_DIR}/wolig.pl -d shemp.dat | ${GZIP} -4 > dout.nouns.shemp.gz
  DEPENDS shemp.dat ${SCRIPTS_DIR}/wolig.pl
  )
  
add_custom_target(
  gzdicts ALL DEPENDS ${GZDICTS}
  )


# patsubst, my donkey for a patsubst (and preferably an inline one, like the
# good old make could do so simply)
# I have no idea why this "simple" thing doesn't work...
#STRING(REGEX REPLACE "^(.*)" "${DATA_DIR}/\\1" OUTPUT ${DICTS})
foreach(adict ${DICTS})
  set(DICTS_FULL ${DICTS_FULL} ${DATA_DIR}/${adict})
endforeach(adict)
# (gzip -dc $(GZDICTS); cat $(DICTS)) | ./pmerge -p hebrew.wgz.tmp | ./wzip | gzip -9n > hebrew.wgz
#        -rm -f hebrew.wgz.prefixes
#        gzip -9n < hebrew.wgz.tmp >hebrew.wgz.prefixes
#        -rm -f hebrew.wgz.tmp
#hebrew.wgz.prefixes: hebrew.wgz
#hebrew.wgz.sizes: hebrew.wgz find_sizes
#        gzip -dc hebrew.wgz | ./find_sizes >hebrew.wgz.sizes


# why can't depends on the target gzdicts??
#add_custom_command(
#  OUTPUT hebrew.wgz hebrew.wgz.tmp
#  COMMAND true && (${GZIP} -dc ${GZDICTS} \; cat ${DICTS_FULL}) | PERL5LIB=${PERL5LIB} ${SCRIPTS_DIR}/pmerge -p hebrew.wgz.tmp | ${SCRIPTS_DIR}/wzip | ${GZIP} -9n > hebrew.wgz
#  DEPENDS ${GZDICTS} $(DICTS) ${SCRIPTS_DIR}/pmerge ${SCRIPTS_DIR}/wzip ${SCRIPTS_DIR}/PrefixBits.pl
#  )

add_custom_command(
  OUTPUT hebrew.wgz.prefixes
  COMMAND ${GZIP} -9n < hebrew.wgz.tmp > hebrew.wgz.prefixes
  DEPENDS hebrew.wgz.tmp
  )

# find_sizes is compiled in src, but not directly from here, so let's just hope
# it's there...
add_custom_command(
  OUTPUT hebrew.wgz.sizes
  COMMAND ${GZIP} -dc hebrew.wgz | ${EXE_DIR}/find_sizes > hebrew.wgz.sizes
  DEPENDS hebrew.wgz ${EXE_DIR}/find_sizes
  )

add_custom_target(
  data ALL DEPENDS hebrew.wgz hebrew.wgz.prefixes hebrew.wgz.sizes
  )


#todo
# add the rest of the configure options







# yes, I have more complaints on cmake...

